﻿using LCL.CodeHelper;
using LCL.IBuilder;
using LCL.LData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LCL.Builder.IBatisNet
{
    //Template\TemplatePro\WebAPI
    public partial class ProjectBuilder
    {
        #region BuilderWebAPI
        private void BuilderWebAPI()
        {
            string SourceDirectory = Utils.GetMapPath(@"Template\TemplatePro\WebAPI");
            string TargetDirectory = CodeCommon.DesktopDirectory + NameSpace + @"/WebAPI";
            CodeCommon.CopyDirectory(SourceDirectory, TargetDirectory);
            var tables = DbObject.GetTables(DbName);
            foreach (var table in tables)
            {
                var Fieldlist = DbObject.GetColumnInfoList(DbName, table.TableName);
                List<ColumnInfo> Keys = Fieldlist.Where(p => p.IsPrimaryKey == true).ToList();
                string className = CodeCommon.GetClassName(table.TableName, TableFormat);
                string ModelSpace = NameSpace + ".Model." + className;//命名空间 + 类名
                string DALSpace = NameSpace + ".DAL." + className;
                string IDALSpace = NameSpace + ".IDAL." + className;
                #region StringBuilder
                StringBuilder builder = new StringBuilder();
                builder.AppendLine("using System; ");
                builder.AppendLine("using System.Collections.Generic; ");
                builder.AppendLine("using System.Linq; ");
                builder.AppendLine("using System.Net; ");
                builder.AppendLine("using System.Net.Http; ");
                builder.AppendLine("using System.Web.Http; ");
                builder.AppendLine("using " + NameSpace + ".Model; ");
                builder.AppendLine("using " + NameSpace + ".IDAL; ");
                builder.AppendLine("using " + NameSpace + ".DALFactory; ");
                builder.AppendLine("namespace " + NameSpace + ".WebAPI ");
                builder.AppendLine("{ ");
                builder.AppendLine("    public class " + className + "Controller : ApiController ");
                builder.AppendLine("    { ");
                builder.AppendLine("        private I" + className + "DAL idal = DataAccess.Create" + className + "(); ");
                builder.AppendLine(" ");
                builder.AppendLine("        // GET检索 api/" + className + " ");
                builder.AppendLine("        public List<" + ModelSpace + "> Get() ");
                builder.AppendLine("        { ");
                builder.AppendLine("            var list = idal.GetListModel(\"\"); ");
                builder.AppendLine("            return list; ");
                builder.AppendLine("        } ");
                builder.AppendLine("        // GET检索 api/" + className + "/5 ");
                builder.AppendLine("        public " + ModelSpace + " Get(" + CodeCommon.GetInParameter(Keys) + ") ");
                builder.AppendLine("        { ");
                builder.AppendLine("            " + ModelSpace + " model = idal.GetModel(" + CodeCommon.GetFieldstrlist(Keys) + "); ");
                builder.AppendLine("            return model; ");
                builder.AppendLine("        } ");
                builder.AppendLine("        // POST创建 api/" + className + "  ");
                builder.AppendLine("        public void Post([FromBody]" + ModelSpace + " model) ");
                builder.AppendLine("        { ");
                builder.AppendLine("            idal.Add(model); ");
                builder.AppendLine("        } ");
                builder.AppendLine("        // PUT更新 api/" + className + "/5 ");
                builder.AppendLine("        public void Put(int id, [FromBody]" + ModelSpace + " model) ");
                builder.AppendLine("        { ");
                builder.AppendLine("            idal.Update(model); ");
                builder.AppendLine("        } ");
                builder.AppendLine("        // DELETE删除 api/" + className + "/5 ");
                builder.AppendLine("        public void Delete(" + CodeCommon.GetInParameter(Keys) + ") ");
                builder.AppendLine("        { ");
                builder.AppendLine("            idal.Delete(" + CodeCommon.GetFieldstrlist(Keys) + "); ");
                builder.AppendLine("        } ");
                builder.AppendLine("    } ");
                builder.AppendLine("} ");

                #endregion
                string filename = TargetDirectory + @"/Controllers/" + className + "Controller.cs";
                CodeCommon.CreateFiles(filename, builder.ToString());
                try
                {
                    VSProject.AddClass2012(TargetDirectory + @"/WebAPI.csproj", @"Controllers\" + className + "Controller.cs");
                    VSProject.AddClass2012(TargetDirectory + @"/WebAPI.csproj", @"Maps\" + className + ".xml");
                }
                catch { }
            }
            try
            {
                VSProject.AddClass2012(TargetDirectory + @"/WebAPI.csproj", @"DataBase.config");
            }
            catch { }
            AddReference2012(TargetDirectory);
            AddProjectReference2012(TargetDirectory);

            DaoCnf(TargetDirectory);
            SqlMap_Oracle_OracleClient(TargetDirectory);
        }

        private void AddReference2012(string TargetDirectory)
        {
            try
            {
                string file = TargetDirectory + @"/WebAPI.csproj";
                VSProject.AddReference2012(file, "LCL.Common", @"..\Lib\LCL.Common.dll");
                VSProject.AddReference2012(file, "MySql.Data", @"..\Lib\MySql.Data.dll");
                VSProject.AddReference2012(file, "Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86", @"..\Lib\Oracle.DataAccess.dll");
                VSProject.AddReference2012(file, "System.Data.SQLite", @"..\Lib\System.Data.SQLite.dll");

                VSProject.AddReference2012(file, "IBatisNet.Common, Version=1.6.1.0, Culture=neutral, PublicKeyToken=ed781d9fc396c6ca, processorArchitecture=MSIL", @"..\Lib\IBatisNet.Common.dll", "False");
                VSProject.AddReference2012(file, "IBatisNet.DataAccess, Version=1.9.1.0, Culture=neutral, PublicKeyToken=ed781d9fc396c6ca, processorArchitecture=MSIL", @"..\Lib\IBatisNet.DataAccess.dll", "False");
                VSProject.AddReference2012(file, "IBatisNet.DataMapper, Version=1.6.1.0, Culture=neutral, PublicKeyToken=ed781d9fc396c6ca, processorArchitecture=MSIL", @"..\Lib\IBatisNet.DataMapper.dll", "False");
            }
            catch
            { }
        }
        private void AddProjectReference2012(string TargetDirectory)
        {
            try
            {
                string file = TargetDirectory + @"/WebAPI.csproj";
                VSProject.AddProjectReference2012(file, NameSpace + ".DAL", DALGuid, @"..\DAL\" + NameSpace + ".DAL.csproj");
                VSProject.AddProjectReference2012(file, NameSpace + ".DALFactory", DALFactoryGuid, @"..\DALFactory\" + NameSpace + ".DALFactory.csproj");
                VSProject.AddProjectReference2012(file, NameSpace + ".IDAL", IDALGuid, @"..\IDAL\" + NameSpace + ".IDAL.csproj");
                VSProject.AddProjectReference2012(file, NameSpace + ".Model", ModelGuid, @"..\Model\" + NameSpace + ".Model.csproj");
            }
            catch
            { }
        }
        #endregion


        private void DaoCnf(string TargetDirectory)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?> ");
            builder.AppendLine("<daoConfig  xmlns=\"http://ibatis.apache.org/dataAccess\"  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> ");
            builder.AppendLine("  <property name=\"UseConfigFileWatcher\" value=\"true\" /> ");
            builder.AppendLine("  <context id=\"SqlMapDao," + NameSpace + ".DAL\" default=\"true\"> ");
            builder.AppendLine("    <properties resource=\"database.config\"/> ");
            builder.AppendLine("    <database> ");
            builder.AppendLine("      <provider name=\"oracleClient1.0\" /> ");
            builder.AppendLine("      <dataSource name=\"iBatisNet\" connectionString=\"User ID=${userid}; Password=${password}; Data Source=${database}\"/> ");
            builder.AppendLine("    </database> ");
            builder.AppendLine("    <daoSessionHandler id=\"SqlMap\"> ");
            builder.AppendLine("      <property name=\"sqlMapConfigFile\" value=\"SqlMap_Oracle_OracleClient.config\" /> ");
            builder.AppendLine("    </daoSessionHandler> ");
            builder.AppendLine("    <daoFactory> ");
            var tables = DbObject.GetTables(DbName);
            foreach (var table in tables)
            {
                string className = CodeCommon.GetClassName(table.TableName, TableFormat);
                builder.AppendLine("      <dao interface=\"" + NameSpace + ".I" + className + "DAL\" ");
                builder.AppendLine("           implementation=\"" + NameSpace + "." + className + "," + NameSpace + ".DAL\" /> ");
            }
            builder.AppendLine("    </daoFactory> ");
            builder.AppendLine("  </context> ");
            builder.AppendLine("</daoConfig> ");

            string filename = TargetDirectory + @"/dao.config";
            CodeCommon.CreateFiles(filename, builder.ToString());

            try
            {
                VSProject.AddClass2012(TargetDirectory + @"/WebAPI.csproj", @"dao.config");
            }
            catch { }
        }

        private void SqlMap_Oracle_OracleClient(string TargetDirectory)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?> ");
            builder.AppendLine("<sqlMapConfig xmlns=\"http://ibatis.apache.org/dataMapper\" ");
            builder.AppendLine("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" > ");
            builder.AppendLine("  <properties resource=\"database.config\"/> ");
            builder.AppendLine("  <settings> ");
            builder.AppendLine("    <setting useStatementNamespaces=\"false\" /> ");
            builder.AppendLine("    <setting cacheModelsEnabled=\"true\" /> ");
            builder.AppendLine("  </settings> ");
            builder.AppendLine("  <database> ");
            builder.AppendLine("    <provider name=\"oracleClient1.0\" /> ");
            builder.AppendLine("    <dataSource name=\"iBatisNet\" connectionString=\"User ID=${userid}; Password=${password}; Data Source=${database}\"/> ");
            builder.AppendLine("  </database> ");
            builder.AppendLine("  <sqlMaps> ");
            var tables = DbObject.GetTables(DbName);
            foreach (var table in tables)
            {
                string className = CodeCommon.GetClassName(table.TableName, TableFormat);
                builder.AppendLine("    <sqlMap resource=\"Maps/" + className + ".xml\" /> ");
            }
            builder.AppendLine("  </sqlMaps> ");
            builder.AppendLine("</sqlMapConfig> ");

            string filename = TargetDirectory + @"/SqlMap_Oracle_OracleClient.config";
            CodeCommon.CreateFiles(filename, builder.ToString());
            try
            {
                VSProject.AddClass2012(TargetDirectory + @"/WebAPI.csproj", @"SqlMap_Oracle_OracleClient.config");
            }
            catch { }
        }
    }
}
